const http = require('http')
const qs=require('querystring')
const config=require('../config') 
const apiaddress=require('./apiaddress')

const getPlayerBalance = async (ctx,next) => { //TCG获取用户余额
    let {coin}=ctx.request.body
    const userId =  ctx.session.userId
    if(userId){
        const result = new Promise((resolve,reject) => {
            let opt={
                hostname:config.aghostname,
                port: config.agport,
                path:`${apiaddress.tcggetBalance}?userId=${userId}&coin=${coin}`,
                method: "GET"
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    resolve(JSON.parse(resdata))
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.end()
        })
        let data=await result
        if(data.code&&data.code!=200){
            ctx.error(data)
        }else{
            ctx.success(data)
        }
    }else{
        let params={
            code:200,
            data:null
        }
        ctx.success(params)
    }
}

const fundTransfer = async (ctx,next) => { //用户兑换、兑出功能 type:1 存款 2 提款
    let {coin,amount,type}=ctx.request.body
    const userId =  ctx.session.userId
    if(userId){
        let params={
            userId:userId,
            coin:coin,
            amount:amount,
            type:type
        }
        let postdata=qs.stringify(params)
        const result=new Promise((resolve,reject) => {
            let opt={
                hostname:config.aghostname,
                port: config.agport,
                path:`${apiaddress.tcgfundTransfer}`,
                method: "POST",
                headers:{
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': postdata.length
                }
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    resolve(JSON.parse(resdata))
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.write(postdata)
            req.end()
        })
        let data=await result
        if(data.code&&data.code!=200){
            ctx.error(data)
        }else{
            ctx.success(data)
        }
    }else{
        let params={
            code:200,
            data:null
        }
        ctx.success(params)
    }
}

const launchGame = async (ctx,next) => { //启动游戏
    let {coin,lottoName,pageName,lottoType}=ctx.request.body
    const userId =  ctx.session.userId
    if(userId){
        const result = new Promise((resolve,reject) => {
            let opt={
                hostname:config.aghostname,
                port: config.agport,
                path:`${apiaddress.tcglaunchGame}?userId=${userId}&coin=${coin}&lottoName=${lottoName}&viewName=${pageName}&lottoType=${lottoType}`,
                method: "GET"
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    let responsedata=JSON.parse(resdata)
                    if(responsedata.code==200){
                        if(responsedata.data.status==0){
                            ctx.session.tcggameurl=responsedata.data.game_url
                        }
                    }
                    resolve(JSON.parse(resdata))
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.end()
        })
        let data=await result
        if(data.code&&data.code!=200){
            ctx.error(data)
        }else{
            ctx.success(data)
        }
    }else{
        let params={
            code:200,
            data:null
        }
        ctx.success(params)
    }
}

const tcgmobilegame=[ //移动端TCG游戏列表
    {
        floorname:'全部',
        data:[
            {
                gamename:'腾讯分分彩',
                code:'TXFFC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TXFFC.png'
            },
            {
                gamename:'加拿大28',
                code:'CA28',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/CA28.png'
            },
            {
                gamename:'台湾28',
                code:'TW28',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TW28.png'
            },
            {
                gamename:'天成28',
                code:'B2BC28',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCG28.png'
            },
            {
                gamename:'北京幸运28',
                code:'BJLK28',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/BJLK28.png'
            },
            {
                gamename:'天成五分彩',
                code:'B2BCSSCC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCGSSC.png'
            },
            {
                gamename:'重庆时时彩',
                code:'CQSSC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/CQSSC.png'
            },
            {
                gamename:'天成分分彩',
                code:'B2BCFFC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCGFFC.png'
            },
            {
                gamename:'新疆时时彩',
                code:'XJSSC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/XJSSC.png'
            },
            {
                gamename:'山东十一选五',
                code:'SD11X5',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/SD11X5.png'
            },
            {
                gamename:'天成十一选五',
                code:'B2BC11X5',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCG11X5.png'
            },
            {
                gamename:'江西十一选五',
                code:'JX11X5',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/JX11X5.png'
            },
            {
                gamename:'福彩3D',
                code:'FC3D',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/FC3D.png'
            },
            {
                gamename:'体彩P3P5',
                code:'TCP3P5',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCP3P5.png'
            },
            {
                gamename:'天成PK10',
                code:'B2BCPK10',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCGPK10.png'
            },
            {
                gamename:'北京赛车PK10',
                code:'BJPK10',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/BJPK10.png'
            },
            {
                gamename:'射手赛车',
                code:'TCSPK10',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/Icon/TC/Default/TCSPK10.png'
            },
            {
                gamename:'幸运飞艇',
                code:'XYPK10',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84//XYPK10.png'
            },
            {
                gamename:'香港⑥合彩',
                code:'HK6',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/HK6.png'
            },
            {
                gamename:'天成六合彩',
                code:'B2BCLHC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCGLHC.png'
            },
            {
                gamename:'天秤分分彩',
                code:'TCFFC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/Icon/TC/Default/TCFFC.png'
            },
            {
                gamename:'双鱼11选5',
                code:'TC11X5',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/Icon/TC/Default/TC11X5.png'
            },
            {
                gamename:'天秤二分彩',
                code:'TCT2FC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/Icon/TC/Default/TCT2FC.png'
            },
            {
                gamename:'上海十一选五',
                code:'SH11X5',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/SH11X5.png'
            },
            {
                gamename:'上海快三',
                code:'SHK3',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/SHK3.png'
            },
            {
                gamename:'天成快三',
                code:'B2BCK3',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCGK3.png'
            },
            {
                gamename:'湖北快三',
                code:'HUBK3',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/HUBK3.png'
            },
            {
                gamename:'广西快三',
                code:'GXK3',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/GXK3.png'
            },
            {
                gamename:'比特股分分彩',
                code:'BTSFFC',
                img:'https://images.uxgaming.com/TCG_GAME_ICONS/Lotto/tcgdemo/BTSFFC.png'
            },
            {
                gamename:'比特股11选5',
                code:'BTS11X5',
                img:'https://images.uxgaming.com/TCG_GAME_ICONS/Lotto/tcgdemo/BTS11X5.png'
            },
            {
                gamename:'比特股28',
                code:'BTS28',
                img:'https://images.uxgaming.com/TCG_GAME_ICONS/Lotto/tcgdemo/BTS28.png'
            },
            {
                gamename:'比特股PK10',
                code:'BTSPK10',
                img:'https://images.uxgaming.com/TCG_GAME_ICONS/Lotto/tcgdemo/BTSPK10.png'
            }
        ]
    },
    {
        floorname:'区块链',
        data:[
            {
                gamename:'比特股分分彩',
                code:'BTSFFC',
                img:'https://images.uxgaming.com/TCG_GAME_ICONS/Lotto/tcgdemo/BTSFFC.png'
            },
            {
                gamename:'比特股11选5',
                code:'BTS11X5',
                img:'https://images.uxgaming.com/TCG_GAME_ICONS/Lotto/tcgdemo/BTS11X5.png'
            },
            {
                gamename:'比特股28',
                code:'BTS28',
                img:'https://images.uxgaming.com/TCG_GAME_ICONS/Lotto/tcgdemo/BTS28.png'
            },
            {
                gamename:'比特股PK10',
                code:'BTSPK10',
                img:'https://images.uxgaming.com/TCG_GAME_ICONS/Lotto/tcgdemo/BTSPK10.png'
            }
        ]
    },
    {
        floorname:'时时彩',
        data:[
            {
                gamename:'腾讯分分彩',
                code:'TXFFC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TXFFC.png'
            },
            {
                gamename:'天成五分彩',
                code:'B2BCSSCC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCGSSC.png'
            },
            {
                gamename:'重庆时时彩',
                code:'CQSSC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/CQSSC.png'
            },
            {
                gamename:'天秤分分彩',
                code:'TCFFC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/Icon/TC/Default/TCFFC.png'
            },
            {
                gamename:'比特股分分彩',
                code:'BTSFFC',
                img:'https://images.uxgaming.com/TCG_GAME_ICONS/Lotto/tcgdemo/BTSFFC.png'
            }
        ]
    },
    {
        floorname:'十一选五',
        data:[
            {
                gamename:'山东十一选五',
                code:'SD11X5',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/SD11X5.png'
            },
            {
                gamename:'天成十一选五',
                code:'B2BC11X5',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCG11X5.png'
            },
            {
                gamename:'江西十一选五',
                code:'JX11X5',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/JX11X5.png'
            },
            {
                gamename:'双鱼11选5',
                code:'TC11X5',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/Icon/TC/Default/TC11X5.png'
            },
            {
                gamename:'上海十一选五',
                code:'SH11X5',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/SH11X5.png'
            },
            {
                gamename:'比特股11选5',
                code:'BTS11X5',
                img:'https://images.uxgaming.com/TCG_GAME_ICONS/Lotto/tcgdemo/BTS11X5.png'
            }
        ]
    },
    {
        floorname:'低频彩',
        data:[
            {
                gamename:'福彩3D',
                code:'FC3D',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/FC3D.png'
            },
            {
                gamename:'体彩P3P5',
                code:'TCP3P5',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCP3P5.png'
            }
        ]
    },
    {
        floorname:'六合彩',
        data:[
            {
                gamename:'香港⑥合彩',
                code:'HK6',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/HK6.png'
            },
            {
                gamename:'天成六合彩',
                code:'B2BCLHC',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCGLHC.png'
            }
        ]
    },
    {
        floorname:'PK10',
        data:[
            {
                gamename:'天成PK10',
                code:'B2BCPK10',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCGPK10.png'
            },
            {
                gamename:'北京赛车PK10',
                code:'BJPK10',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/BJPK10.png'
            },
            {
                gamename:'射手赛车',
                code:'TCSPK10',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/Icon/TC/Default/TCSPK10.png'
            },
            {
                gamename:'幸运飞艇',
                code:'XYPK10',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84//XYPK10.png'
            },
            {
                gamename:'比特股PK10',
                code:'BTSPK10',
                img:'https://images.uxgaming.com/TCG_GAME_ICONS/Lotto/tcgdemo/BTSPK10.png'
            }
        ]
    },
    {
        floorname:'PC蛋蛋',
        data:[
            {
                gamename:'加拿大28',
                code:'CA28',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/CA28.png'
            },
            {
                gamename:'台湾28',
                code:'TW28',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TW28.png'
            },
            {
                gamename:'天成28',
                code:'B2BC28',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCG28.png'
            },
            {
                gamename:'北京幸运28',
                code:'BJLK28',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/BJLK28.png'
            },
            {
                gamename:'比特股28',
                code:'BTS28',
                img:'https://images.uxgaming.com/TCG_GAME_ICONS/Lotto/tcgdemo/BTS28.png'
            }
        ]
    },
    {
        floorname:'快三',
        data:[
            {
                gamename:'上海快三',
                code:'SHK3',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/SHK3.png'
            },
            {
                gamename:'天成快三',
                code:'B2BCK3',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/TCGK3.png'
            },
            {
                gamename:'湖北快三',
                code:'HUBK3',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/HUBK3.png'
            },
            {
                gamename:'广西快三',
                code:'GXK3',
                img:'http://images.uxgaming.com/TCG_PROD_IMAGES/B2B/LOTT/Default/template2/84x84/GXK3.png'
            }
        ]
    }
]

module.exports={
    getPlayerBalance,
    fundTransfer,
    launchGame,
    tcgmobilegame
}